<?php


namespace member\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;
use herosphp\core\Loader;


class ContractAction extends CommonAction
{
    protected $serviceBean = "trading.tradingContract.service";
    protected $member_id=null;

    public function __construct(){
        parent::__construct();
        $this->member_id = $this->_user->__get('id');
    }

    public function index(HttpRequest $request)
    {
        $contractService = Beans::get($this->serviceBean);
        $orderService = Beans::get('trading.tradingOrder.service');

        $page = $request->getParameter('page');
        if($page<=0) $page = 1;
        $pagesize = 10;
        $this->setPagesize($pagesize);
        $this->setPage($page);


        $search_arr = array(
            "select_mix_option" => $request->getParameter('select_mix_option'),
            "select_mix_option_value" => urldecode($request->getParameter('select_mix_option_value')),
            "order_way" => $request->getParameter('order_way'),
            "status" => $request->getParameter('status'),
            "start_time" => $request->getParameter('start_time'),
            "end_time" => $request->getParameter('end_time'),
            "member_id" => $this->_user->id,

        );


        $this->assign('search_arr', $search_arr);

        $arr = $contractService->search($pagesize, $page, $search_arr);

        $items = $arr['items'];

        $itemsLen = count($items);

        for ($i = 0; $i < $itemsLen; $i++) {

            $items[$i]['status'] = $contractService->getStatusText($items[$i]['status']);

            $items[$i]['progress'] = $contractService->getProgressText($items[$i]['progress']);
            $items[$i]['order_way'] = $orderService->getPaywayText2($items[$i]['pay_way']);
        }

        $this->memberPagemenu($arr['total']);

        $this->assign('total', $arr['total']);

        $this->assign('items', $items);
        $this->assign('search_arr', $search_arr);
        $this->assign('pagesize', $pagesize);

        $this->assign('_table', '合同');
        $this->assign('_filter', array('contract_no', 'sellers', 'buyers', 'order_no', 'signed_time', 'type', 'status'));
        $this->assign('_header', array('合同号', '卖家', '买家', '订单号', '签订时间', '类型', '状态'));

        $this->assign("title", "合同列表");
        $this->assign('bread', array('交易管理', $this->getTemplateVar('title')));
        $this->assign('breadUrl', array(url('admin'), 'javascript:;'));

        $requestType = $request->getParameter('ajax');
        if($requestType) {
            $this->setView(strtolower($request->getAction()) . '/contract_index_page');
        }else {
            $this->setView(strtolower($request->getAction()) . '/contract_index');
        }
    }

    public function details(HttpRequest $request)
    {

        $id = $request->getParameter('id');
        $contractModel = Loader::model('tradingContract');
        $item = $contractModel->getItem($id);

        $item ? AjaxResult::ajaxResult('000', '', $item) : AjaxResult::ajaxFailtureResult();
    }
    public function txt(HttpRequest $request) {
        $id = $request->getParameter('id');
        $contractModel = Loader::model('tradingContract');
        $item = $contractModel->getItem(array('order_id'=>$id));
        $orderService = Beans::get('trading.tradingOrder.service');
        if(!$orderService->checkOrder($id,$this->member_id)){
            return $this->message('您没有权限查看此合同','/member/order/index');
        }
        $order_item = $orderService->getDetail($id);
        $orderModel = Loader::model('tradingOrder');
        if($order_item['order_status'] != $orderModel::ORDER_STATUS_COMPLETED){
            return $this->message('该订单尚未交易完成','/member/order/index');
        }
        $total_quantity = 0;
        $order_product = array();
        foreach($order_item['product'] as $k=>$v){
            $total_quantity += $v['product_quantity'];
            $attribute = json_decode($v['attribute'],true);
            $order_product[$k]['pkg_size'] = $attribute['pkg_size'];//包装规格
            $order_product[$k]['type_name'] = $attribute['type_name'];//类型
            $order_product[$k]['factory_name'] = $attribute['factory_name'];//厂家
            $order_product[$k]['product_name'] = $attribute['product_name'];//品名
            $order_product[$k]['brand_name'] = $attribute['brand_name'];//牌号
            $order_product[$k]['level'] = $attribute['level'];//质量等级
            $order_product[$k]['product_price'] = $v['product_price'];//单价
            $order_product[$k]['product_quantity'] = $v['product_quantity'];//数量
        }
        $order_item['total_quantity'] = setFloat($total_quantity,3);
        $memberService = Beans::get('member.member.service');
        $member_info = $memberService->getUsernameById($order_item['member_id']);
        //var_dump($order_item);
        $item['final_y'] = $order_item['pay_way']==$orderModel::PAY_WAY_ONLINE?date('Y',strtotime($item['final_payment_time'])):'';
        $item['final_m'] = $order_item['pay_way']==$orderModel::PAY_WAY_ONLINE?date('m',strtotime($item['final_payment_time'])):'';
        $item['final_d'] = $order_item['pay_way']==$orderModel::PAY_WAY_ONLINE?date('d',strtotime($item['final_payment_time'])):'';
        $order_item['shipping']['y'] = date('Y',strtotime($order_item['shipping']['shipping_time']));
        $order_item['shipping']['m'] = date('m',strtotime($order_item['shipping']['shipping_time']));
        $order_item['shipping']['d'] = date('d',strtotime($order_item['shipping']['shipping_time']));
        $this->assign('item', $item);
        $this->assign('order_item', $order_item);
        $this->assign('order_product', $order_product);
        $this->assign('member_info', $member_info);
        $this->assign('PAY_WAY_ONLINE', $orderModel::PAY_WAY_ONLINE);
        $this->assign('SHIPPING_WAY_SELLER', $orderModel::SHIPPING_WAY_SELLER);
        $this->setView(strtolower($request->getAction()) .'/contract_txt');
    }

}